﻿#include <windows.h>
#include <setupapi.h>
#include <iostream>
#include <vector>
#include <fcntl.h>
#include <io.h>
#include <devguid.h>  // 包含设备GUID定义

#pragma comment(lib, "setupapi.lib")

struct DeviceInfo {
    std::wstring deviceId;
    std::wstring description;
    std::wstring friendlyName;
    std::wstring hardwareIds;
    std::wstring manufacturer;
    std::wstring driverDate;
    std::wstring driverVersion;
};

std::vector<DeviceInfo> EnumerateAllDevices() {

    std::vector<DeviceInfo> devices;
    
    // 获取所有设备类
    HDEVINFO hDevInfo = SetupDiGetClassDevs(
        NULL,                   // 所有类GUID
        NULL,                   // 所有枚举器
        NULL,                   // 父窗口句柄
        DIGCF_ALLCLASSES | DIGCF_PRESENT
    );
    
    if (hDevInfo == INVALID_HANDLE_VALUE) {
        return devices;
    }
    
    SP_DEVINFO_DATA devInfoData;
    devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    
    for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData); i++) {
        DeviceInfo deviceInfo;
        
        // 获取设备ID
        WCHAR deviceId[256];
        if (SetupDiGetDeviceInstanceId(hDevInfo, &devInfoData, deviceId, 
                                      sizeof(deviceId)/sizeof(WCHAR), NULL)) {
            deviceInfo.deviceId = deviceId;
        }
        
        // 获取设备描述
        WCHAR description[256];
        DWORD dataType;
        DWORD size = sizeof(description);
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
                                            SPDRP_DEVICEDESC, &dataType,
                                            (PBYTE)description, size, &size)) {
            deviceInfo.description = description;
        }
        
        // 获取友好名称
        WCHAR friendlyName[256];
        size = sizeof(friendlyName);
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
                                            SPDRP_FRIENDLYNAME, &dataType,
                                            (PBYTE)friendlyName, size, &size)) {
            deviceInfo.friendlyName = friendlyName;
        }
        
        // 获取制造商
        WCHAR manufacturer[256];
        size = sizeof(manufacturer);
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
                                            SPDRP_MFG, &dataType,
                                            (PBYTE)manufacturer, size, &size)) {
            deviceInfo.manufacturer = manufacturer;
        }
        
        // 获取硬件ID
        WCHAR hardwareIds[1024];
        size = sizeof(hardwareIds);
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
                                            SPDRP_HARDWAREID, &dataType,
                                            (PBYTE)hardwareIds, size, &size)) {
            deviceInfo.hardwareIds = hardwareIds;
        }
        
        devices.push_back(deviceInfo);
    }
    
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return devices;
}


std::vector<DeviceInfo> EnumerateDevicesByClass(const GUID& classGuid) {
    std::vector<DeviceInfo> devices;
    
    HDEVINFO hDevInfo = SetupDiGetClassDevs(
        &classGuid,
        NULL,
        NULL,
        DIGCF_PRESENT
    );
    
    if (hDevInfo == INVALID_HANDLE_VALUE) {
        return devices;
    }
    
    SP_DEVINFO_DATA devInfoData;
    devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    
    for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData); i++) {
        DeviceInfo deviceInfo;
        

        // 获取设备描述
        WCHAR description[256];
        DWORD dataType;
        DWORD size = sizeof(description);
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
                                            SPDRP_DEVICEDESC, &dataType,
                                            (PBYTE)description, size, &size)) {
            deviceInfo.description = description;
        }

          // 获取友好名称
        WCHAR friendlyName[256];
        size = sizeof(friendlyName);
        if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData,
                                            SPDRP_FRIENDLYNAME, &dataType,
                                            (PBYTE)friendlyName, size, &size)) {
            deviceInfo.friendlyName = friendlyName;
        }

        // 获取设备信息...
        // 同上例
        
        devices.push_back(deviceInfo);
    }
    
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return devices;
}

// 使用示例
void EnumerateSpecificDeviceClasses() {
    // 磁盘驱动器
    auto diskDrives = EnumerateDevicesByClass(GUID_DEVCLASS_DISKDRIVE);

     for(auto &item : diskDrives)
    {
        std::wcout << item.friendlyName.c_str() << std::endl;
    }
    
    // 显示适配器
    auto displayAdapters = EnumerateDevicesByClass(GUID_DEVCLASS_DISPLAY);
    
    // 网络适配器
    auto networkAdapters = EnumerateDevicesByClass(GUID_DEVCLASS_NET);
    
    // 处理器
    auto processors = EnumerateDevicesByClass(GUID_DEVCLASS_PROCESSOR);
    
    // USB设备
    auto usbDevices = EnumerateDevicesByClass(GUID_DEVCLASS_USB);
}


int main(int argc, const char** argv) {
    _setmode(_fileno(stdout), _O_U16TEXT);
    
    EnumerateSpecificDeviceClasses();
    return 0;
}

